const express = require('express');

const router = express.Router();

const Station = require('../model/station')

// 添加站点
router.post('/add', async (req, res) => {
    // uid不能通过客户发送过来
    // uid都应该从token中获取
    const {
        stationAdd,
        stationName,
        cityName,
        stationGps,
    } = req.body;
    console.log(req);

    // 数据过滤
    if (!stationName) return res.send({ success: false, info: '请填写站点名称' });
    if (!cityName) return res.send({ success: false, info: '请填写站点的城市名字' });
    if (!stationAdd) return res.send({ success: false, info: '请填写站点地址' });
    if (!stationGps) return res.send({ success: false, info: '请设置站点GPS信息' });
    

    try {
        // 执行入库操作
        await Station.create({
            stationName,
            cityName,
            stationAdd,
            stationGps,
        });
        res.send({ success: true, info: '添加成功' });
    } catch (error) {
        res.send({ success: false, info: '添加失败' });
    }

})

// 获取全部站点
router.post('/getAll', async (req, res) => {

    let { stationName, cityName, page, limit } = req.body;
    
    page = page || 1; //当前第几页
    limit = limit || 20; //单页返回的条数限制

    // 初始化查询条件
    let where = {};

    if (stationName) where.stationName = stationName;
    if (cityName) where.cityName = cityName;


    const skip = (page - 1) * limit; //查询的起点（偏移量）
    
    try {
        let stations = await Station.find(where, {}, { skip, limit }) //分页查询
        let count=await Station.count(where)  //获取符合条件的总数

        res.send({success:true,info:'查询成功',data:stations,count})
    } catch (error) {
        console.log(error);
        res.send({success:false,info:'查询失败'})
    }
})

// 获取单个站点信息
router.post('/getOne', async (req, res) => {

    const { stationId } = req.body;

    if (!stationId) return res.send({ success: false, info: '请传入stationId' })
    
    try {
        const station = await station.findById(orderId);
        res.send({success:true,info:'获取成功',data:station})
    } catch (error) {
        console.log(error);
        res.send({success:false,info:'获取失败'})
    }
})

// 修改站点信息http://127.0.0.1:3002/api/v1/station/edit
router.post('/edit', async (req, res) => {

    const {
        stationName,
        cityName,
        stationAdd,
        stationGps,
        stationId
    } = req.body;
    
    if (!stationId) return res.send({ success: false, info: '请传入stationId' })

    // 初始化要变更的数据
    let updateDate={}

    if (stationName) updateDate.stationName = stationName;
    if (cityName) updateDate.cityName = cityName;
    if (stationAdd) updateDate.stationAdd = stationAdd;
    if (stationGps) updateDate.stationGps = stationGps;

    try {
        await Station.findByIdAndUpdate(stationId, updateDate)
        res.send({success:true,info:'修改成功'})
    } catch (error) {
        res.send({success:false,info:'修改失败'})
    }
})

// 删除站点信息
router.post('/del', async (req, res) => {
    const { id } = req.body;
    try {
        await Station.findByIdAndDelete(id);
        res.send({success:true,info:'删除成功'})

    } catch (error) {
        res.send({success:false,info:'删除失败'})
    }
})

module.exports = router;